﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Reflection;
using Common.Resource;
using RockCompany.Common.Constant;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;

namespace RockCompany.Common.Lib
{
    public class Translator
    {
        private Font vnFont, chnFont, curFont;

        private CommonConstantIF.Language nLocation = CommonConstantIF.Language.VN;
        public CommonConstantIF.Language LocationLang
        {
            get { return nLocation; }
            set { nLocation = value; }
        }

        public Translator()
        {
            vnFont = new Font("Arial", 8);
            chnFont = new Font("SimSun", 9);
        }

        public string getMessageCaption(string key)
        {
            string s = "";
            switch (nLocation)
            {
                case CommonConstantIF.Language.VN:
                    s = MessageCaption_VN.ResourceManager.GetString(key);
                    break;
                case CommonConstantIF.Language.CH:
                    s = MessageCaption_CH.ResourceManager.GetString(key);
                    break;
            }
            return s;
        }

        public string getControlLabel(string key)
        {
            string s = "";
            switch (nLocation)
            {
                case CommonConstantIF.Language.VN:
                    s = ControlLabel_VN.ResourceManager.GetString(key);
                    break;
                case CommonConstantIF.Language.CH:
                    s = ControlLabel_CH.ResourceManager.GetString(key);
                    break;
            }
            return s;
        }

        public void controlTranslate(Form fParentForm)
        {
            switch (nLocation)
            {
                case CommonConstantIF.Language.VN:
                    curFont = vnFont;
                    break;
                case CommonConstantIF.Language.CH:
                    curFont = chnFont;
                    break;
            }

            fParentForm.Text = getControlLabel(fParentForm.Name.ToUpper() + "_" + "TITLE");

            for (int i = 0; i < fParentForm.Controls.Count; i++)
            {
                Control c = fParentForm.Controls[i];

                if (c.GetType() == typeof(DataGridView) || c.GetType() == typeof(GroupBox) || c.GetType() == typeof(Label) || c.GetType() == typeof(Button) || c.GetType() == typeof(RadioButton) || c.GetType() == typeof(CheckBox))
                {
                    c.Font = curFont;
                    string sCtrlID = fParentForm.Controls[i].Name.ToUpper();
                    string sKey = fParentForm.Name + "_" + sCtrlID;
                    if (c.GetType() == typeof(DataGridView))
                    {
                        DataGridView dg = (DataGridView)c;
                        dg.RowsDefaultCellStyle.Font = vnFont;
                        for (int j = 0; j < dg.ColumnCount; j++)
                        {
                            if (dg.Columns[j].Visible)
                            {
                                string colName = dg.Columns[j].Name;
                                dg.Columns[colName].HeaderText = getControlLabel(fParentForm.Name.ToUpper() + "_" + colName.ToUpper());
                            }
                        }
                    }
                    else if (c.GetType() == typeof(GroupBox))
                    {
                        GroupBox gb = (GroupBox)c;
                        sCtrlID = gb.Name.ToUpper();
                        sKey = fParentForm.Name + "_" + sCtrlID;

                        gb.Text = getControlLabel(sKey);

                        foreach (Control cc in gb.Controls)
                        {
                            sCtrlID = cc.Name.ToUpper();
                            sKey = fParentForm.Name + "_" + sCtrlID;

                            string oldTxt = cc.Text;
                            string newTxt = getControlLabel(sKey);
                            cc.Text = (newTxt == null ? oldTxt : newTxt);
                            if (cc.GetType() == typeof(Label) || cc.GetType() == typeof(Button) || cc.GetType() == typeof(RadioButton) || cc.GetType() == typeof(CheckBox))
                            {
                                cc.Font = curFont;
                            }
                            else
                                cc.Font = vnFont;
                        }
                    }
                    else
                    {
                        string oldTxt = c.Text;
                        string newTxt = getControlLabel(sKey);
                        c.Text = (string.IsNullOrEmpty(newTxt) ? oldTxt : newTxt);
                    }
                }
                else
                    c.Font = vnFont;
            }
        }
    }
}
